<?php


namespace App\Modules\Order\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class getContributionOrderServiceListCriteria extends BaseCriterias
{
    private $params;

    public function  __construct($storeId,$barberId,$startDate,$endDate,$isProfit=0)
    {
        $this->params = [
            'store_id' => $storeId,
            'barber_id' => $barberId,
            'start_date' =>$startDate,
            'end_date'  => $endDate,
            'is_profit' => $isProfit
        ];
    }

    public function apply($model, RepositoryInterface $repository)
    {
    	$price = $this->params['is_profit'] ? 'profit' : 'real_price';
        return $model->select(['id','finish_time',$price])->with(['items'=>function($v){
        	$v->with(['service'=>function($query){
		        $query->select(['id','type'])->with(['category'=>function($v){
			            $v->select(['id','resource'])->with('picture:id,image');
		        }]);
	        }]);
        }])
            ->whereRaw('store_id ="' . $this->params['store_id'] . '" and barber_id="'.$this->params['barber_id'].'" and FROM_UNIXTIME(finish_time,\'%Y-%m-%d\') <= "' .$this->params['start_date']. '" and FROM_UNIXTIME(finish_time,\'%Y-%m-%d\') >="' . $this->params['end_date'] . '"');

   }
}
